<?php

namespace app\modules\frontend\controllers;

use app\models\SignupForm;
use Yii;
use yii\filters\AccessControl;
use yii\helpers\Url;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use yii\web\UploadedFile;
use app\models\Park;

class SiteController extends Controller
{
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    public function actionIndex()
    {
        $parks=Park::find()->all();
        return $this->render('index',['parks'=>$parks,]);
        //return $this->render('index');
    }

    public function actionUser()
    {
        return $this->render('user');
    }
    /*public function actionLogin()
    {
        if (!\Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        }
        return $this->render('login', [
            'model' => $model,
        ]);
    }*/
	public function actionLogin()
    {
        $this->layout="login";
        if (!\Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            $this->layout="main";

            if($model->type==1) {
                return $this->render('index');
            }
            if($model->type==2){
                return $this->redirect(Url::toRoute('/backend/site/index'));
            }
        } else {
            return $this->render('login', [
                'model' => $model,
            ]);
        }
    }

    public function actionLogout()
    {
        Yii::$app->user->logout();
        $model=new LoginForm();
        return $this->render('/site/about',['model'=>$model]);
    }

    public function actionContact()
    {
        $model = new ContactForm();
        if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
            Yii::$app->session->setFlash('contactFormSubmitted');

            return $this->refresh();
        }
        return $this->render('contact', [
            'model' => $model,
        ]);
    }

    public function actionAbout()
    {
        return $this->render('about');
    }
    public function actionTest()
    {
        return $this->render('test');
    }

    public function actionSignup()
    {
        $model = new SignupForm();
        /* if ($model->load(Yii::$app->request->post())) {
             if ($user = $model->signup()) {
                 if (Yii::$app->getUser()->login($user)) {
                     return $this->goHome();
                 }
             }
         }*/
        if (Yii::$app->request->isPost) {
            echo "<br><br><br><br><br><br><br><br>";

            $model->imageFile = UploadedFile::getInstance($model, 'imageFile');
            $model->imageFile2 = UploadedFile::getInstance($model, 'imageFile2');

            if ($model->upload()) {
                var_dump("文件上传成功");
                return;
            }
        }

        return $this->render('signup', [
            'model' => $model,
        ]);
    }
}
